
$(function () {
    $(".nav h2").on("click", function () {
    $(this).next().slideToggle();  
})
})

window.onload = function () {
    let box = document.querySelector(".box");
    let right = document.querySelector(".box .right");
    box.onclick = function (e) {
        e = e || Event;
        if (e.target.classList.contains('souye')) {
            right.innerHTML = `
            <img src="../data/img/logo.jpg" alt="">
                <h1>欢迎来到宿舍管理系统</h1>
            `
        }
        if (e.target.classList.contains("record")) {
            record()
        }
        if (e.target.classList.contains("quit")) {
            let flag = confirm("确认退出宿舍管理系统？");
            if (!flag) return;
            location.href = "../index.html";
        }
        if (e.target.classList.contains("gai")) {
            change();
        }
    }
    async function record() {
        let res =await pAjax({
            url: `/api/project/recodedata.php`,
        })
        res = JSON.parse(res);
        rundent1(res)
    }
    function rundent1(res) {
        let user = getCookie('login');
        let nux = '';
        res.forEach(function (item) {
            console.log(item);
            if (item.user == user) {
                
                 nux+= `
                    <tr data="${item.id}">
                                <td>${item.menhao}</td>
                                <td>${item.user}</td>
                                <td>${item.description}</td>
                                <td>${item.newtime}</td>
                                <td>${item.oldtime==null?"":item.oldtime}</td>
                                <td><button id="zz" class="${item.state=="申请"?"sq":item.state=="受理"?"sl":item.state=="完成"?"wc":"bh"}">${item.state}</button></td>
                                <td>${item.remark==null?"":item.remark}</td>
                            </tr>
                `
            }
                
        });
        right.innerHTML = `
        <div class="supervisor weixiu">
            <div class="supervisor_t">
                    <button class="apply">申请</button>
                    <label for="" class="lable1">查询方式：</label>
                    <select name="" id="select">
                        <option value="1">寝室号</option>
                        <option value="2">申请时间</option>
                        <option value="4">状态查询</option>
                    </select>
                    <input type="text" class="shuru">
                    <button class="chaxun">查询</button>
                    <button class="all">查看全部</button>


                </div>
                <div class="addapply">
                    <div class="zzc">
                        <form action="" id="studentform">
                            <p>维修申请</p>
                            <p class="yz">
                                <label for="">寝室号：</label>
                                <input type="text" placeholder="请输入寝室号" name="user" id="userid" required>
                            </p>
                            <p class="xx">
                                 <label for="">保修描述：</label>
                                 <textarea rows="10" cols="30" placeholder="请输入维修描述" id="text"></textarea>
                                
                            </p>
                           
                            <p>
                                <button id="applyadd">提交申请</button>
                                <span class="cancel">取消</span>
                            </p>
                        </form>
                    </div>
                </div>
         <div class="supervisor_f2">
                    <table>
                        <thead>
                            <tr>
                                <th>寝室</th>
                                <th>学号</th>
                                <th>维修描述</th>
                                <th>申请时间</th>
                                <th>维修时间</th>
                                <th>当前状态</th>
                                <th>备注</th>
                            </tr>
                        </thead>
                        <tbody>
                            ${nux}
                        </tbody>
                        <tfoot></tfoot>
                    </table>
                </div>
                
            </div>
        `;
        $('.right .apply').on("click", function () {
        $('.addapply').show();
            $('#studentform').animate({
                left: ($(".right").width() - $("#studentform").width()) / 2,
                top: ($(".right").height() - $("#studentform").height()) / 2
            }, 0);
            $(".right .cancel").on("click", function () {
                $('.addapply').hide();
                $(".right #studentform #userid").val("");
                $(".right #studentform #text").val("");
            });
            function formatDate(d) {
            var year = d.getFullYear();
            var month = d.getMonth() + 1;
            month = month < 10 ? "0" + month : month;

            var date = d.getDate();
            date = date < 10 ? "0" + date : date;


            return {
                year: year,
                month: month,
                date: date,
            };
            }
            $(".right #applyadd").on("click", function (e) {
                e.preventDefault();
                let mh = $(".right #studentform #userid").val();
                let text = $(".right #studentform #text").val();
                if (mh == "" || text == "") {
                    alert("所有字段均不能为空");
                    return;
                }
                let flag = confirm("是否确认提交改申请？");
                if (!flag) return;
                let data = new Date();
                let xx = formatDate(data);
                time = `${xx.year}-${xx.month}-${xx.date}`;
                let user = getCookie('login');
                ajax({
                    url: `/api/project/recodeadd.php`,
                    data:{
                        menhao: mh,
                        userid: user,
                        text: text,
                        time:time
                    },
                    success: function (res) {
                        res = JSON.parse(res);
                        if (res.code == 1) {
                            alert("申请提交成功");
                            record();
                            $('.addapply').hide();
                            $(".right #studentform #userid").val("");
                            $(".right #studentform #text").val("");
                        }
                    }
                })
            });
            
        
    
        });
        $(".weixiu .chaxun").on("click", function () {
            let way = $("#select").val();
            let text = $(".weixiu .shuru").val();
            console.log(way);
            console.log(text);
            if (text == '') {
                alert("请输入关键字进行查询");
                return;
            }
            async function select() {
                let res = await pAjax({
                    url: `/api/project/recodeselect.php`,
                    data: {
                        way: way,
                        text: text
                    },
                    type: "post",
                })
                res = JSON.parse(res);
                rundent1(res);
            }
            select()
        });
        $(".weixiu .all").on("click", function () {
            record();
        })


    }

    function change() {
        $(".box .mima").show();
        $('#mi').animate({
            left: ($(".right").width() - $("#mi").width()) / 2,
            top: ($(".right").height() - $("#mi").height()) / 2
        }, 0);
        jQuery.validator.addMethod(
                "userTestx",
                function (value) {
                    let user = getCookie("login");
                    if (value == user) {
                        return true;
                    } else {
                        return false;
                    }
                },
                "用户名不正确"
        );
        jQuery.validator.addMethod(
                "passTest",
            function (value) {
                let flag = true;
                ajax({
                    url: `/api/project/studentdata.php`,
                    async: false,
                    success: function (res) {
                        let user = getCookie("login");
                        res = JSON.parse(res).list;
                        for (let i = 0; i < res.length; i++){
                            console.log(res[i].id);
                            if (res[i].id == user && res[i].password == value) {
                                flag = true; break;
                            } else {
                                flag = false; continue;
                            }
                        } 
                    }
                })
                
                return flag;
                },
                "原密码不正确"
                );


         $("#mi").validate({
            rules: {
                user: {
                    required: true,
                    userTestx:true
                },
                old: {
                    required: true,
                    passTest:true
                    
                },
                new: {
                    required: true,

                },
                agein: {
                    required: true,
                    equalTo:"#new"
 
                },
                
             },
             messages: {
                 agein: {
                     equalTo:"两次密码不一致"
                 }

             },
             submitHandler: function () {
                 let user = getCookie("login");
                 let password = $(".box .mima #mi #new").val();
                 console.log(password);
                async function tj() {
                    let res = await pAjax({
                        url: `/api/project/xiustudentupdata.php`,
                        data: {
                            user: user,
                            password:password
                        },
                    })
                    console.log(res);
                    res = JSON.parse(res);
                    if (res.code == true) {
                        alert("修改密码成功");
                        $(".box .mima").hide();
                        location.href = "../html/index.html";
                    }
                 }
                 tj();
            },
         });
        
        
        $(".box .mima #cel").on("click", function () {
            $(".box .mima").hide();
        })
    }
}

